package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppDeviceRegister;
import com.irdstudio.apicenter.console.dm.AppFrameComm;
import com.irdstudio.apicenter.console.service.appframe.AppFrameCommService;
import com.irdstudio.apicenter.console.service.authenticate.AppDeviceRegisterService;
import com.irdstudio.apicenter.core.utils.StringUtil;
import com.irdstudio.apicenter.core.utils.web.http.HttpClientUtil;

@Controller
@RequestMapping(value = "/appDeviceRegister")
public class AppDeviceRegisterAction extends MacmsBaseAction {
	@Autowired
	private AppFrameCommService appFrameCommService;
	@Autowired
	private AppDeviceRegisterService appDeviceRegisterService;

	// 插入跳转
	@RequestMapping(value = "jumpAddDevice")
	public String jumpAddDevice(HttpServletRequest request) {
		String appid = request.getParameter("appid");
		request.setAttribute("appid", appid);
		return "auth/addDevice";
	}

	// 插入保存
	@RequestMapping(value = "addDevice")
	public void addDevice(
			@ModelAttribute("appDeviceRegister") AppDeviceRegister appDeviceRegister,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appDeviceRegisterService.insertOne(appDeviceRegister);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 更新跳转
	@RequestMapping(value = "jumpUpdateDevice")
	public String jumpUpdateDevice(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String appId = request.getParameter("appId");
		String deviceId = request.getParameter("deviceId");
		AppDeviceRegister appDeviceRegister = appDeviceRegisterService
				.queryAppTradePrivate(appId, deviceId);
		request.setAttribute("appDeviceRegister", appDeviceRegister);
		request.setAttribute("appid", appId);
		return "auth/updateDevice";
	}

	// 更新保存
	@RequestMapping(value = "updateDevice")
	public void updateDevice(
			@ModelAttribute("appDeviceRegister") AppDeviceRegister appDeviceRegister,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		// 查询原来的配置
		String appId = appDeviceRegister.getAppId();
		String deviceId = appDeviceRegister.getDeviceId();
		AppDeviceRegister appDeviceRegister_old = appDeviceRegisterService
				.queryAppTradePrivate(appId, deviceId);
		String isAllow_old = appDeviceRegister_old.getIsAllow();
		int flag = -1;
		flag = appDeviceRegisterService.update(appDeviceRegister);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		// 推送消息到移动设备
		String isAllow = appDeviceRegister.getIsAllow();
		// 当修改了准入值则推送
		if (!isAllow_old.equals(isAllow)) {
			AppFrameComm appFrameComm = appFrameCommService
					.queryAppFrameCommDetail(appId);
			String maccsUrl = appFrameComm.getAppCommUrl();
			Map<String, String> postData = new HashMap<String, String>();
			postData.put("app_id", appId);
			postData.put("user_id", appDeviceRegister_old.getAppUserid());
			postData.put("pk_value", new Date().getTime() + "");
			postData.put("push_params", "");
			postData.put("push_type", StringUtil.gbEncoding("系统提醒"));
			String masContent = "";
			// 开通设备准入
			if (isAllow_old.equals("N") && isAllow.equals("Y")) {
				masContent = "您的设备已通过审核，可以正常使用了哦！";
			}
			// 关闭设备准入
			else if (isAllow_old.equals("Y") && isAllow.equals("N")) {
				masContent = "您的设备已关闭准入，需继续使用app请联系管理员！";
			}
			postData.put("push_alter_msg", StringUtil.gbEncoding(masContent));
			try {
				HttpClientUtil.doPost(maccsUrl + appId + "/ma_push_msg.do",
						postData, "GBK", true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 删除
	@RequestMapping(value = "deleteDevice")
	public void deleteDevice(
			@ModelAttribute("appDeviceRegister") AppDeviceRegister appDeviceRegister,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appDeviceRegisterService.delete(appDeviceRegister);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 查看
	@RequestMapping(value = "viewDevice")
	public String viewDevice(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String appId = request.getParameter("appid");
		String deviceId = request.getParameter("deviceId");
		AppDeviceRegister appDeviceRegister = appDeviceRegisterService
				.queryAppTradePrivate(appId, deviceId);
		request.setAttribute("appDeviceRegister", appDeviceRegister);
		request.setAttribute("appid", appId);
		return "auth/viewDevice";
	}
}
